<template>
	<view style="padding-bottom:50rpx;">
		<page-head :title="'我的陪驾'" :headtype="2" :gourl="'/pages/tabbar/my'"></page-head>
		<view class="tagnav">
			<view class="tagnavmain marginAuto" style="width: 50%;">
				<view class="naveach" @click="navfun(1)" :class="navac == 1 ? 'navactive':''">我的套餐
					<view class="naveachx"></view>
				</view>
				<view class="naveach" @click="navfun(2)" :class="navac == 2 ? 'navactive':''">我的预约
					<view class="naveachx"></view>
				</view>
			</view>
		</view>
		<view class="tagnav" v-if="navac == 2">
			<view class="tagnavmain marginAuto" style="width:90%;">
				<view class="naveach" @click="yynavfun(1)" :class="yynavac == 1 ? 'navactive':''">待派单
					<view class="naveachx"></view>
				</view>
				<!-- 服务中订单体验课后台添加-上传券码  +完成按钮（正式课） -->
				<view class="naveach" @click="yynavfun(2)" :class="yynavac == 2 ? 'navactive':''">待服务
					<view class="naveachx"></view>
				</view>
				<view class="naveach" @click="yynavfun(3)" :class="yynavac == 3 ? 'navactive':''">已完成
					<view class="naveachx"></view>
				</view>
				<view class="naveach" @click="yynavfun(4)" :class="yynavac == 4 ? 'navactive':''">已取消
					<view class="naveachx"></view>
				</view>
			</view>
		</view>
		<view class="drlist">
			<!-- @click="apifun.navigate('/pages/my/mymealxq','id='+item.id)" -->
			<view class="dritem marginAuto" v-if="navac == 1" v-for="(item,index) in tcdatas" :key="index">
				<view class="dritemgoods flex">
					<view class="cardeachbimg">
						<image class="goodsimg" :src="apifun.URLimg+item.menu_image" mode=""></image>
					</view>
					<view class="cardeachbmsg">
						<view class="cardeachbmsg-name flex">
							<view class="cardeachbmsg-namen ellipsis01">{{ item.menu_name }}</view>
							<view class="cardeachbmsg-namestatus mainColor" v-if="item.residue_duration > 0">剩余{{ item.residue_duration }}小时</view>
							<view class="cardeachbmsg-namestatus" style="color: #149E22;" v-if="item.residue_duration <= 0">已用完</view>
						</view>
						<view class="starty">
							<view class="jlq">{{ item.min }}小时起约</view>
						</view>
						<view class="cardeachbmsg-jl">
							<text style="color:#FC2A2A;font-size:24rpx;">￥</text>
							<text style="color:#FC2A2A;font-size:36rpx;">{{ item.menu_price }}</text>
							<text style="color:#666666;font-size:24rpx;margin-left: 20rpx;text-decoration: line-through;">￥{{ item.menu_old_price }}</text>
						</view>
						<view class="cardeachbmsg-site flex">
							<view class="site flex">有效期：{{ item.menu_end_time }}</view>
							<view class="cardbtn flex" v-if="item.pay_status == 2">
								<view class="cardbtneach mainBg baiColor" @click.stop="appointmentfun(item,index)">去预约</view>
							</view>
							<view class="cardbtn flex" v-if="item.pay_status == 1">
								<view class="cardbtneach mainBg baiColor" @click.stop="payfun(item,index)">支付</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="dritem marginAuto" v-if="navac == 2" v-for="(item,index) in yydatas" :key="index" @click="apifun.navigate('/pages/my/mydrivermakexq','id='+item.id)">
				<view class="dritem-top flex"> 
					<view class="dritemtl flex">
						<!-- <view class="jlq">{{ item.duration }}小时</view> -->
						<view class="dritemtltxt">练车时间：{{ item.reserve_time_txt }}</view>
					</view>
					<view class="dritemtr mainColor">{{ item.order_status_txt }}</view>
				</view>
				<view class="dritem-top flex">
					<view class="dritemtl flex">
						<!-- <view class="jlq">{{ item.duration }}小时</view> -->
						<view class="dritemtltxt">预约时长：{{ item.duration }}小时</view>
					</view>
				</view>
				<!-- 练车时长 -->
				<view class="dritemgoods flex">
					<view class="cardeachbimg">
						<image class="xxhf" v-if="item.pay_type == 2" :src="apifun.serveimg+'xxhf.png'" mode="heightFix"></image>
						<image class="goodsimg" :src="apifun.URLimg+item.menu_image" mode=""></image>
					</view>
					<view class="cardeachbmsg">
						<view class="cardeachbmsg-name flex">
							<view class="cardeachbmsg-namen ellipsis01">{{ item.menu_name }}</view>
							<view class="cardeachbmsg-namestatus mainColor">剩余{{ item.residue_duration }}小时</view>
						</view>
						<view class="cardeachbmsg-jl" style="margin-bottom:30rpx;margin-top: 10rpx;">
							<text style="color:#FC2A2A;font-size:24rpx;">￥</text>
							<text style="color:#FC2A2A;font-size:36rpx;">{{ item.menu_price }}</text>
							<!-- <text style="color:#666666;font-size:24rpx;margin-left: 20rpx;text-decoration: line-through;">￥{{ item.menu_old_price }}</text> -->
						</view>
						<!-- <view class="cardeachbmsg-site flex">
							<view class="site flex ellipsis02" style="height:100rpx;">陪驾地址:{{ item.address }}</view>
						</view> -->
						<view class="cardeachbmsg-site flex">
							<view class="site flex">订单号：{{ item.order_sn }}</view>
						</view>
					</view>
				</view>
				<view class="drbtn flex">
					<view></view>
					<view class="flex">
						<!-- status：1待派单 2待确认 3待服务 4待评价 5已完成 6已取消 7待验券 8待上传券码   is_trial  1体验课-->
						<block v-if="yynavac == 1">
							<view class="cardbtn flex">
								<view class="cardbtneach mainColor" @click.stop="closeorderfun(item,index)">取消</view>
							</view>
							<view class="cardbtn flex">
								<view class="cardbtneach mainBg baiColor" @click.stop="apifun.navigate('/pages/my/mydriveryy','orderid='+item.id+'&mealid='+item.menu_id+'&mealorderid='+item.order_id)">修改</view>
							</view>
						</block>
						<block v-if="yynavac == 2 && item.source_id == 1">
							<view class="cardbtn flex">
								<view class="cardbtneach mainColor" @click.stop="closeorderfun(item,index)">取消</view>
							</view>
							<view class="cardbtn flex">
								<view class="cardbtneach mainColor" @click.stop="apifun.navigate('/pages/my/mydriveryy','orderid='+item.id+'&mealid='+item.menu_id+'&mealorderid='+item.order_id)">修改</view>
							</view>
							<view class="cardbtn flex">
								<view class="cardbtneach mainBg baiColor" @click.stop="endorderfun(item,index)">完成</view>
							</view>
						</block>
						<block v-if="yynavac == 2 && item.source_id != 1">
							<view class="cardbtn flex" >
								<view class="cardbtneach mainColor" @click.stop="$refs.isupqcode.open(),acitem=item,acindex=index">上传券码</view>
							</view>
						</block>
						<block v-if="yynavac == 3 && item.is_trial == 1">
							<view class="cardbtn flex" >
								<view class="cardbtneach mainColor" @click.stop="apifun.navigate('/pages/accompany/maketest')">测评一下</view>
							</view>
							<view class="cardbtn flex">
								<view class="cardbtneach mainColor" @click.stop="apifun.navigate('/pages/my/mydrivermakeev','jlid='+item.coach_user_id)">评价</view>
							</view>
						</block>
						<block v-if="yynavac == 3 && item.is_trial != 1">
							<view class="cardbtn flex" >
								<view class="cardbtneach mainColor" @click.stop="apifun.navigate('/pages/accompany/maketest')">测评一下</view>
							</view>
							<view class="cardbtn flex">
								<view class="cardbtneach mainColor" @click.stop="moremakefun(item,index)">再次预约</view>
							</view>
							<view class="cardbtn flex">
								<view class="cardbtneach mainColor" @click.stop="apifun.navigate('/pages/my/mydrivermakeev','jlid='+item.coach_user_id)">评价</view>
							</view>
						</block>
						
						<block v-if="yynavac == 4">
							<view class="cardbtn flex">
								<view class="cardbtneach mainColor" @click.stop="moremakefun(item,index)">再次预约</view>
							</view>
						</block>
					</view>
				</view>
			</view>
		</view>
		<!-- 我的套餐弹窗 -->
		<uv-popup ref="seletype" bgColor="none" round="10">
			<view class="seletype">
				<view class="seletypetitle">选择预约方式</view>
				<view class="seletypebtn mainBg baiColor" @click="randomfun()">随机预约</view>
				<view class="seletypebtn" @click="direct()">教练直约</view>
			</view>
		</uv-popup>
		<!-- 购买 -->
		<view class="bottompopup">
			<view class="zg" v-if="iscarpay" @click="iscarpay = false"></view>
			<view class="pup" :class="iscarpay ? 'showtop':''" style="width:100%;">
				<view class="pupclose" @click="iscarpay = false">
					<uv-icon name="close" size="22" color="#222222"></uv-icon>
				</view>
				<view class="paypup">
					<view class="money"><text style="font-size: 20rpx;">￥</text>{{ acitem.amount }}</view>
					<view class="paytype">
						<view class="paytypetitle marginAuto">支付方式</view>
						<view class="paytypemain marginAuto">
							<view class="paytypeeach marginAuto flex" @click="paytype = 1">
								<view class="paytypeetitle">微信支付</view>
								<view class="paytypeer">
									<uv-icon name="checkmark-circle-fill" size="22" :color="paytype == 1 ? '#1686F8':'#E5E5E5'"></uv-icon>
								</view>
							</view>
							<view class="paytypeeach marginAuto flex" @click="paytype = 2">
								<view class="paytypeetitle">先用后付<text style="color: #9E9E9E;margin-left:16rpx;">0元下单，验车完成之后支付</text></view>
								<view class="paytypeer">
									<uv-icon name="checkmark-circle-fill" size="22" :color="paytype == 2 ? '#1686F8':'#E5E5E5'"></uv-icon>
								</view>
							</view>
							<view class="paytypeeach marginAuto flex" @click="apifun.navigate('/pages/home/discount','ordersn='+acitem.order_sn+'&apisite=/api/partnerDrive/getPartnerDriveOrderCouponList')">
								<view class="paytypeetitle">优惠券</view>
								<view class="paytypeer flex">
									<view class="rtxt" style="color:#999999;" v-if="!disitem.id">请选择</view>
									<view class="rtxt" style="color:#333;" v-if="disitem.id">￥{{ disitem.amount }}</view>
									<uv-icon name="arrow-right" size="16" color="#999999"></uv-icon>
								</view> 
							</view>
						</view>
					</view> 
				</view>
				<view class="marginAuto" style="width:600rpx;margin:60rpx auto;">
					<uv-button text="立即支付" @click="enterpay()" :customStyle="{
						'background':'#1686F8',
						'color':'#ffffff',
						'font-size':'30rpx',
						'width':'600rpx',
						'height':'100rpx',
						'border-radius':'100rpx'
					}"></uv-button>
				</view>
			</view>
		</view>
		<!-- 弹窗 -->
		<uv-popup ref="isupqcode" bgColor="none" round="20">
			<view class="closetest" style="height: 800rpx;background: #fff;">
				<image class="closetestbg" style="height:600rpx;" :src="apifun.serveimg+'closetestbg.png'" mode=""></image>
				<view class="closetesttitle">上传券码</view>
				<view class="closetestdoc">
					<input class="closetestdocinput" type="text" placeholder="请输入券码" v-model="upqcodetxt"/>
				</view>
				<view class="uploadvolume marginAuto flex" style="margin-top: 20rpx;" @click="uploadvolume">
					<view class="uploadvolumeicon">
						<uv-icon  name="photo" size="40" color="#ccc"></uv-icon>
					</view>
					<image :src="apifun.URLimg+volumesrc" mode="aspectFill"></image>
				</view>
				<view class="closetestbtn flex">
					<view class="closetestbtne mainColor" style="background: #ffffff;" @click="$refs.isupqcode.close()">取消</view>
					<view class="closetestbtne mainBg baiColor" @click="upqcodefun()">确定</view>
				</view>
			</view>
		</uv-popup>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				apifun:this.apifun,
				page:1,
				navac:1,
				yynavac:1,
				acitem:{},
				acindex:'',
				tcdatas:[],
				yydatas:[],
				// 我的套餐-支付
				iscarpay:false,
				paytype:1,
				disitem:{},  // 优惠券
				upqcodetxt:'',
				volumesrc:''
			};
		},
		onLoad(opt) {
			this.navac = opt.navac || 1;
			if(this.navac == 1){
				this.getmymealfun();
			}else{
				this.getmyappointmentfun()
			}
			uni.$on('emitdiscount',(data)=>{  // 优惠券
				this.disitem = data.disitem;
			});
		},
		onShow() {
			if(this.$refs.seletype){
				this.$refs.seletype.close();
			}
		},
		methods: {
			enterpay(){  // 支付
				let sendData = {
					user_coupon_id:this.disitem.id || '',
					pay_type:this.paytype,
					order_sn:this.acitem.order_sn || ''
				}
				this.apifun.unirequest('/api/partnerDrive/payPartnerDriveOrder','post',sendData,(res)=>{
					// console.log(res)
					if(res.code === 200){
						let datas = res.data;
						this.iscarpay = false;
						this.apifun.wxpaymini(datas,()=>{
							this.apifun.navigate('/pages/accompany/mealpaysuc');
						})
					}else{
						this.apifun.toast(res.msg)
					}
				})
			},
			getmymealfun(){  // 我的套餐
				let sendData = {
					page:this.page,
					limit:this.apifun.limit
				};
				this.apifun.unirequest('/api/partnerDrive/getMyPartnerDriveList','post',sendData,(res)=>{
					// console.log(res)
					if(res.code === 200){
						let datas = res.data;
						if(datas.data.length <= 0){
							this.page--;
							return false
						};
						this.tcdatas = [...this.tcdatas,...datas.data];
					}else{
						this.apifun.toast(res.msg)
					}
				})
			},
			getmyappointmentfun(){  // 我的预约
				let sendData = {
					page:this.page,
					limit:this.apifun.limit,
					type:this.yynavac
				};
				this.apifun.unirequest('/api/partnerDrive/getMyPartnerDriveReserveList','post',sendData,(res)=>{
					if(res.code === 200){
						let datas = res.data;
						if(datas.data.length <= 0){
							this.page--;
							return false
						};
						this.yydatas = [...this.yydatas,...datas.data];
					}else{
						this.apifun.toast(res.msg)
					}
				})
			},
			// -------------套餐
			appointmentfun(item,index){  // 预约
				this.acitem = item;
				this.acindex = index;
				this.$refs.seletype.open();
			},
			payfun(item,index){  // 支付
				this.acitem = item;
				this.acindex = index;
				this.iscarpay=true;
			},
			randomfun(){  // 随机预约
				this.$refs.seletype.close();
				this.apifun.navigate('/pages/my/mydriveryy','mealid='+this.acitem.menu_id+'&menuname='+this.acitem.menu_name)
			},
			direct(){  //  直接预约
				this.$refs.seletype.close();
				this.apifun.navigate('/pages/my/mydriverzylist','mealid='+this.acitem.menu_id)
			},
			// --------------预约
			closeorderfun(item,index){  // 取消
				this.acitem = item;
				this.acindex = index;
				let sendData = {
					id:item.id
				};
				uni.showModal({
					title: '取消预约',
					content: '确定要取消预约吗？',
					success:(res)=>{
						if (res.confirm) {
							this.apifun.unirequest('/api/partnerDrive/cancel','post',sendData,(res)=>{
								if(res.code === 200){
									let datas = res.data;
									this.yydatas.splice(index,1)
								}else{
									this.apifun.toast(res.msg)
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			endorderfun(item,index){  // 完成 
				this.acitem = item;
				this.acindex = index;
				let sendData = {
					id:item.id
				};
				uni.showModal({
					title: '完成预约',
					content: '确定要完成预约吗？',
					success:(res)=>{
						if (res.confirm) {
							this.apifun.unirequest('/api/partnerDrive/complete','post',sendData,(res)=>{
								if(res.code === 200){
									let datas = res.data;
									this.yydatas.splice(index,1)
								}else{
									this.apifun.toast(res.msg)
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			upqcodefun(){
				let sendData = {
					order_id:this.acitem.id,
					code:this.upqcodetxt,
					img:this.volumesrc
				};
				if(this.upqcodetxt == '' && this.volumesrc == ''){
					this.apifun.toast('请输入券码')
					return false
				}
				this.apifun.unirequest('/api/partnerDrive/uploadCode','post',sendData,(res)=>{
					if(res.code === 200){
						let datas = res.data;
						this.yydatas.splice(this.acindex,1);
						this.$refs.isupqcode.close();
					}else{
						this.apifun.toast(res.msg)
					}
				})
			},
			moremakefun(item,index){  //  再次预约
				let params = 'mealid='+item.menu_id+'&menuname='+item.menu_name+'&jlid='+item.coach_user_id+'&mealorderid='+item.order_id+'&lng='+item.lng+'&lat='+item.lat+'&address='+item.address+'&name='+item.username+'&remark='+item.remark;
				this.apifun.navigate('/pages/my/mydriveryy',params);
				// uni.switchTab({
				// 	url:'/pages/tabbar/accompany'
				// })
			},
			uploadvolume(){
				this.apifun.uploadFileimg((res)=>{
					if(res.code == 200){
						this.volumesrc = res.data.src;
					}
				})
			},
			// public
			navfun(type){
				if(type == this.navac) return false;
				this.navac = type;
				this.yynavac = 1;
				this.page = 1;
				if(this.navac == 1){
					this.tcdatas = [];
					this.getmymealfun();
				}else{
					this.yydatas = [];
					this.getmyappointmentfun();
				}
			},
			yynavfun(type){  // 切换二级导航
				if(type == this.yynavac) return false;
				this.yynavac = type;
				this.page = 1;
				this.yydatas = [];
				this.getmyappointmentfun();
			}
		},
		onPullDownRefresh() {
			this.page = 1;
			if(this.navac == 1){
				this.tcdatas = [];
				this.getmymealfun();
			}else{
				this.yydatas = [];
				this.getmyappointmentfun();
			}
			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			this.page++;
			if(this.navac == 1){
				this.getmymealfun();
			}else{
				this.getmyappointmentfun();
			}
		},
	};
</script>

<style scoped lang="scss">
	@import '@/public/alert.scss';
	@import '../../public/popupb.scss';
	
	.tagnav{
		position:sticky;
		top: 0;
		padding:16rpx 0rpx 24rpx;
		background: #fff;
		.tagnavmain{
			width:90%;
			display: flex;
			justify-content: space-between;
			.naveach {
				position: relative;
				color: #999999;
				font-size:30rpx;
				text-align: center;
				font-weight: 600;
				.naveachx {
					position: absolute;
					bottom: -10rpx;
					left: 50%;
					background: #fff;
					width: 50rpx;
					margin-left: -25rpx;
					height: 6rpx;
					border-radius: 100rpx;
					transition: background .5s;
				}
			}
			.navactive {
				color:#333333;
				
				.naveachx {
					background: #2AB4FF;
				}
			}
		}
	}
	// 列表
	.dritem{
		width: 690rpx;
		padding:10rpx;
		background-color: #fff;
		margin-top: 20rpx;
		border-radius: 20rpx;
		.dritem-top{
			justify-content: space-between;
			padding: 10rpx 0;
			.dritemtl{
				.jlq{
					display: inline-block;
					padding:0 10rpx;
					background:#FFEFE4;
					color:#FF720C;
					font-size:20rpx;
					border-radius:6rpx;
					height:50rpx;
					line-height:50rpx;
					margin-right: 10rpx;
				}
				.dritemtltxt{
					font-size: 28rpx;
					color: #222222;
					line-height: 50rpx;
				}
			}
			.dritemtr{
				font-size: 26rpx;
				line-height: 50rpx;
			}
		}
		.dritemgoods{
			padding: 20rpx 0;
			.cardeachbimg{
				position: relative;
				width: 240rpx;
				height: 210rpx;
				.xxhf{
					position: absolute;
					z-index: 10;
					left: -10rpx;
					top:8rpx;
					height: 50rpx;
				}
				.goodsimg{
					width: 240rpx;
					height: 210rpx;
					border-radius:10rpx;
				}
			}
			.cardeachbmsg{
				flex: 1;
				margin-left: 20rpx;
				.cardeachbmsg-name{
					color: #333333;
					font-size:30rpx;
					line-height: 50rpx;
					justify-content: space-between;
					.cardeachbmsg-namen{
						width:60%;
						font-weight: 600;
					}
					.cardeachbmsg-namestatus{
						font-size: 26rpx;
					}
				}
				.starty{
					padding:5rpx 0;
					.jlq{
						display: inline-block;
						padding:0 10rpx;
						background:#E7F2FF;
						color:#0089FB;
						font-size:20rpx;
						border-radius:6rpx;
						height: 40rpx;
						line-height: 40rpx;
						margin-right: 10rpx;
					}
				}
				.cardeachbmsg-jl{
					padding-top: 10rpx;
					font-size: 26rpx;
					line-height:40rpx;
				}
				.cardeachbmsg-site{
					justify-content: space-between;
					line-height:50rpx;
					.site{
						font-size: 24rpx;
						color:#999999;
						line-height:50rpx;
					}
					.cardbtn{
						.cardbtneach{
							width: 120rpx;
							height: 50rpx;
							line-height: 50rpx;
							font-size:28rpx;
							text-align: center;
							border-radius: 100rpx;
							margin-left: 10rpx;
						}
					}
				}
			}
		}
		.drbtn{
			width: 100%;
			justify-content: space-between;
			.cardbtneach{
				width: 140rpx;
				height: 50rpx;
				line-height: 50rpx;
				font-size:28rpx;
				text-align: center;
				border-radius: 100rpx;
				margin-left: 20rpx;
				border: 1px solid #1686F8;
			}
		}
	}
	.seletype{
		width: 520rpx;
		padding: 40rpx;
		background: #fff;
		.seletypetitle{
			font-size: 40rpx;
			color: #222222;
			text-align: center;
		}
		.seletypebtn{
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			margin-top: 30rpx;
			background: #F4F4F4;
			border-radius:10rpx;
		}
	}
	// --支付方式popup
	.paypup{
		.money{
			padding: 80rpx 0 40rpx;
			color: #222222;
			font-size: 60rpx;
			text-align: center;
		}
		.paytype{
			.paytypetitle{
				width: 690rpx;
				font-size: 28rpx;
				color: #222222;
			}
			.paytypemain{
				.paytypeeach{
					width: 630rpx;
					margin-top: 20rpx;
					padding: 20rpx 30rpx;
					background:#F6F6F6;
					border-radius: 20rpx;
					justify-content: space-between;
					.paytypeetitle{
						color: #222222;
						font-size: 30rpx;
						line-height: 50rpx;
					}
					.paytypeer{
						.rtxt{
							font-size:30rpx;
						}
					}
				}
			}
		}
	}
	.closetestdoc{
		width: 90%;
		background: #fff;
		border-radius: 20rpx;
		margin:40rpx auto 0 !important;
		.closetestdocinput{
			width: 90%;
			padding: 0 5%;
			height:80rpx;
			line-height: 80rpx;
			color: #333333;
			font-size: 28rpx;
			text-align: left;
		}
	}
	.uploadvolume{
		position: relative;
		z-index: 10;
		width:350rpx;
		height:350rpx;
		background: #fff;
		border-radius:20rpx;
		.uploadvolumeicon{
			position: absolute;
			left: 50%;
			top: 50%;
			margin-top: -30rpx;
			margin-left: -30rpx;
		}
		image{
			position: absolute;
			top: 0;
			left: 0;
			z-index: 10;
			width: 100%;
			height: 100%;
			
		}
	}
</style>